package idv.loveloser.servlet;

import idv.loveloser.dao.MdlCourseModules;
import idv.loveloser.dao.MdlCourseModulesDAO;
import idv.loveloser.dao.MdlUrl;
import idv.loveloser.dao.MdlUrlDAO;
import idv.loveloser.util.Util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class GetCourseUrl extends HttpServlet {
	private MdlCourseModulesDAO courseModulesDAO = new MdlCourseModulesDAO();
	private MdlUrlDAO urlDAO = new MdlUrlDAO();
	/**
	 * Constructor of the object.
	 */
	public GetCourseUrl() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		String client = request.getParameter("client");
		String sessionKey = request.getParameter("sessionKey");
//		String courseId=request.getParameter("courseId");
		String urlId=request.getParameter("urlId");
		MdlUrl url=null;
//		String sequence = request.getParameter("sequence");
		List<MdlCourseModules> selectedModules=new ArrayList<MdlCourseModules>();
		List<MdlUrl> urls = new ArrayList<MdlUrl>();
		PrintWriter out = response.getWriter();
		if(Util.isAuthorised(client, sessionKey)){
			url=urlDAO.findById(Long.parseLong(urlId));
		}
		Gson gson = new Gson();
		out.println(gson.toJson(url));
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
